/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package xcore.model.impl;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.EObjectResolvingEList;
import org.eclipse.emf.ecore.util.EcoreUtil;

import xcore.constraint.XIntVar;

import xcore.model.ModelPackage;
import xcore.model.XClass;
import xcore.model.XRefFeature;
import xcore.model.XReference;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>XRef Feature</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link xcore.model.impl.XRefFeatureImpl#getXReference <em>XReference</em>}</li>
 *   <li>{@link xcore.model.impl.XRefFeatureImpl#getFrom <em>From</em>}</li>
 *   <li>{@link xcore.model.impl.XRefFeatureImpl#getTo <em>To</em>}</li>
 *   <li>{@link xcore.model.impl.XRefFeatureImpl#getCardinality <em>Cardinality</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class XRefFeatureImpl extends EObjectImpl implements XRefFeature {
	/**
	 * The cached value of the '{@link #getFrom() <em>From</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFrom()
	 * @generated
	 * @ordered
	 */
	protected XClass from;

	/**
	 * The cached value of the '{@link #getTo() <em>To</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getTo()
	 * @generated
	 * @ordered
	 */
	protected EList<XClass> to;

	/**
	 * The cached value of the '{@link #getCardinality() <em>Cardinality</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getCardinality()
	 * @generated
	 * @ordered
	 */
	protected XIntVar cardinality;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected XRefFeatureImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return ModelPackage.Literals.XREF_FEATURE;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public XReference getXReference() {
		if (eContainerFeatureID != ModelPackage.XREF_FEATURE__XREFERENCE) return null;
		return (XReference)eContainer();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetXReference(XReference newXReference, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newXReference, ModelPackage.XREF_FEATURE__XREFERENCE, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setXReference(XReference newXReference) {
		if (newXReference != eInternalContainer() || (eContainerFeatureID != ModelPackage.XREF_FEATURE__XREFERENCE && newXReference != null)) {
			if (EcoreUtil.isAncestor(this, newXReference))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newXReference != null)
				msgs = ((InternalEObject)newXReference).eInverseAdd(this, ModelPackage.XREFERENCE__FEATURES, XReference.class, msgs);
			msgs = basicSetXReference(newXReference, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ModelPackage.XREF_FEATURE__XREFERENCE, newXReference, newXReference));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public XClass getFrom() {
		if (from != null && from.eIsProxy()) {
			InternalEObject oldFrom = (InternalEObject)from;
			from = (XClass)eResolveProxy(oldFrom);
			if (from != oldFrom) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, ModelPackage.XREF_FEATURE__FROM, oldFrom, from));
			}
		}
		return from;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public XClass basicGetFrom() {
		return from;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setFrom(XClass newFrom) {
		XClass oldFrom = from;
		from = newFrom;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ModelPackage.XREF_FEATURE__FROM, oldFrom, from));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<XClass> getTo() {
		if (to == null) {
			to = new EObjectResolvingEList<XClass>(XClass.class, this, ModelPackage.XREF_FEATURE__TO);
		}
		return to;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public XIntVar getCardinality() {
		return cardinality;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetCardinality(XIntVar newCardinality, NotificationChain msgs) {
		XIntVar oldCardinality = cardinality;
		cardinality = newCardinality;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, ModelPackage.XREF_FEATURE__CARDINALITY, oldCardinality, newCardinality);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setCardinality(XIntVar newCardinality) {
		if (newCardinality != cardinality) {
			NotificationChain msgs = null;
			if (cardinality != null)
				msgs = ((InternalEObject)cardinality).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - ModelPackage.XREF_FEATURE__CARDINALITY, null, msgs);
			if (newCardinality != null)
				msgs = ((InternalEObject)newCardinality).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - ModelPackage.XREF_FEATURE__CARDINALITY, null, msgs);
			msgs = basicSetCardinality(newCardinality, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ModelPackage.XREF_FEATURE__CARDINALITY, newCardinality, newCardinality));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case ModelPackage.XREF_FEATURE__XREFERENCE:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetXReference((XReference)otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case ModelPackage.XREF_FEATURE__XREFERENCE:
				return basicSetXReference(null, msgs);
			case ModelPackage.XREF_FEATURE__CARDINALITY:
				return basicSetCardinality(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eBasicRemoveFromContainerFeature(NotificationChain msgs) {
		switch (eContainerFeatureID) {
			case ModelPackage.XREF_FEATURE__XREFERENCE:
				return eInternalContainer().eInverseRemove(this, ModelPackage.XREFERENCE__FEATURES, XReference.class, msgs);
		}
		return super.eBasicRemoveFromContainerFeature(msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case ModelPackage.XREF_FEATURE__XREFERENCE:
				return getXReference();
			case ModelPackage.XREF_FEATURE__FROM:
				if (resolve) return getFrom();
				return basicGetFrom();
			case ModelPackage.XREF_FEATURE__TO:
				return getTo();
			case ModelPackage.XREF_FEATURE__CARDINALITY:
				return getCardinality();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case ModelPackage.XREF_FEATURE__XREFERENCE:
				setXReference((XReference)newValue);
				return;
			case ModelPackage.XREF_FEATURE__FROM:
				setFrom((XClass)newValue);
				return;
			case ModelPackage.XREF_FEATURE__TO:
				getTo().clear();
				getTo().addAll((Collection<? extends XClass>)newValue);
				return;
			case ModelPackage.XREF_FEATURE__CARDINALITY:
				setCardinality((XIntVar)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case ModelPackage.XREF_FEATURE__XREFERENCE:
				setXReference((XReference)null);
				return;
			case ModelPackage.XREF_FEATURE__FROM:
				setFrom((XClass)null);
				return;
			case ModelPackage.XREF_FEATURE__TO:
				getTo().clear();
				return;
			case ModelPackage.XREF_FEATURE__CARDINALITY:
				setCardinality((XIntVar)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case ModelPackage.XREF_FEATURE__XREFERENCE:
				return getXReference() != null;
			case ModelPackage.XREF_FEATURE__FROM:
				return from != null;
			case ModelPackage.XREF_FEATURE__TO:
				return to != null && !to.isEmpty();
			case ModelPackage.XREF_FEATURE__CARDINALITY:
				return cardinality != null;
		}
		return super.eIsSet(featureID);
	}

} //XRefFeatureImpl
